package com.mlog.njyjy.ndop.process.admin.controller;

import com.mlog.njyjy.mp.starter.maws.response.Resp;
import com.mlog.njyjy.ndop.common.domain.NodeGroup;
import com.mlog.njyjy.ndop.process.admin.service.NodeGroupService;
import com.mlog.njyjy.ndop.process.admin.support.enums.NodeRelationType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author zyz
 * @version v1.0, 2020/8/18
 */
@Api(value = "节点组管理")
@RestController
@CrossOrigin
@RequestMapping("/pm/nodeGroups")
public class NodeGroupController {

    @Autowired
    private NodeGroupService service;

    @ApiOperation(value = "获取节点组树结构", tags = "节点组管理")
    @GetMapping
    public Resp findAll() {
        List<NodeGroup> nodeGroupList = service.treeData();
        if (nodeGroupList != null) {
            return Resp.succeed(nodeGroupList);
        }
        return Resp.fail(404, "未查询到节点组");
    }

    @ApiOperation(value = "增加节点组", tags = "节点组管理")
    @PostMapping
    public Resp addNodeGroup(@RequestParam long baseNodeId,
                             @RequestParam NodeRelationType type,
                             @RequestParam String groupName) {
        boolean success = service.addNodeGroup(baseNodeId, type, groupName);
        return success ? Resp.OK : Resp.fail(500, "创建失败");
    }


    @ApiOperation(value = "删除节点组", tags = "节点组管理")
    @DeleteMapping
    public Resp deleteNodeGroup(@RequestParam long baseNodeId) {
        boolean success = service.deleteNodeGroup(baseNodeId);
        return success ? Resp.OK : Resp.fail(500, "删除失败");
    }

    @ApiOperation(value = "模糊查询节点组", tags = "节点组管理")
    @GetMapping("/search")
    public Resp freezeQueryByName(@RequestParam String input) {
        List<NodeGroup> nodeGroupList = service.freezeQueryByName(input);
        if (nodeGroupList != null) {
            return Resp.of(nodeGroupList);
        }
        return Resp.fail(500, "查询失败");
    }
}
